"""
Train our average svm model.
"""

import numpy as np
from gensim.models import KeyedVectors
from utils.doc_tool import *
from utils.error_analysis import *

# load in our data
w2v = KeyedVectors.load_word2vec_format('w2v/Lyric_ChineseEmbedding.txt',binary=False)

# load in our training data and val data
train_data = dataset2mat(np.load('Dataset/data/train.npy'), w2v)
val_data = dataset2mat(np.load('Dataset/data/val.npy'), w2v)

# compute the average and trainsform it into numpy
avg_train = []
train_label = []
avg_val = []
val_label = []

for i in range(len(train_data)):
    avg_train.append(train_data[i][0].mean(axis=0)) 
    train_label.append(int(train_data[i][1]))
for i in range(len(val_data)):
    avg_val.append(val_data[i][0].mean(axis=0))
    val_label.append(int(val_data[i][1]))
                           
model = SVC(kernel='rbf',decision_function_shape ='ovr',gamma=5.,C=1.0)
model.fit(avg_train, train_label)
pred_y = model.predict(avg_val)
pred_y = np.array(pred_y)
true_y = np.array(val_label)
error_analysis(pred_y, true_y)
                        

